// pages/shortMsg/components/phoneList/phoneList.js
import * as API from '../../../../api/msgApi';

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    'id': {
      type: String
    },
    reachTime: {
      type: String,
      value: '',
      observer: function (reachTime) {
        let oldTime = this.data.reachTimes;
        if (reachTime && reachTime != oldTime) {
          this.setData({
            reachTimes: reachTime
          })
          if (this.data.hasMore) {
            this.setData({
              pageNo: ++this.data.pageNo
            });
            this.loadData(false);
          }
        }
      }
    },
    'customId': {
      type: String
    },
    pageNo: {
      type: Number,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    id: '',
    dataSource: [],
    pageNo: 1,
    pageSize: 10,
    total: 0,
    hasMore: false,
    hasRequest: false,
    networkError: false,
    customId: '',
    reachTime:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    clear: function () {
      // 清空数据重新加载
      this.setData({
        dataSource: [],
        pageNo: 1,
        pageSize: 10,
        hasMore: false
      });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    loadData: function (options) {
      API.getPhoneList(this.data.id, this.data.customId, this.data.pageNo, this.data.pageSize).then(data => {
        let arr = data.customAccountList.map(o => {
          return {
            id: o.id,
            name: o.customAccountName,
            telephone: o.telephone
          };
        }) || [];
        if (!options) {
          arr = this.data.dataSource.concat(data.customAccountList.map(o => {
            return {
              id: o.id,
              name: o.customAccountName,
              telephone: o.telephone
            };
          }))
        };
        let hasMore = arr.length < data.totalCount;
        this.setData({
          dataSource: arr,
          hasMore: hasMore,
          hasRequest: true,
          total: data.totalCount,
          networkError: false
        });
      }, err => {
        this.setData({
          networkError: true
        });
      });
    },
  },
  ready: function () {
    let params = this.id.split(',')
    this.setData({
      id: params[0],
      customId: params[1]
    });
    this.clear();
    this.loadData(true);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 重新加载
    this.clear();
    this.loadData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 增量加载
    if (this.data.hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData(false);
    }
  }
})